package com.zh.business.controller.web.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.zh.business.service.web.user.IDLevelInfoService;
import com.zh.zhbootcommon.domain.DPage;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.entity.user.LevelInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * VIp等级表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Slf4j
@RestController
@RequestMapping("/le/v1")
@Api(value = "VIp等级")
@AopLog(type = "VIp等级", stackTraceOnErr = true)
public class DLevelInfoController {

    @Autowired
    IDLevelInfoService levelInfoService;

    @ApiOperation(value = "VIp等级列表查询接口")
    @PostMapping("/queryLevelPage")
    public DResult queryLevelPage(@RequestParam int page, @RequestParam int limit, @RequestParam String levelText){
        Page<LevelInfo> levelInfoPage = levelInfoService.queryLevelPage(page, limit, levelText);
        DPage<LevelInfo> levelInfoDPage = DPage.to(levelInfoPage);
        return DResult.of(levelInfoDPage);
    }

    @ApiOperation(value = "VIp等级列表查询单个接口")
    @PostMapping("/queryLevelOne")
    public DResult queryLevelOne(@RequestParam Integer levelId){
        return DResult.of(levelInfoService.queryLevelOne(levelId));
    }

    @Transactional
    @ApiOperation(value = "VIp等级列表编辑接口")
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody LevelInfo levelInfo) {
        boolean flag = false;
        if (levelInfo.getLevelId() < 0) {
            // 新增
            flag = levelInfoService.save(levelInfo);
        } else if (levelInfo.getLevelId()  > 0) {
            // 更新
            flag = levelInfoService.updateById(levelInfo);
        }
        return DResult.of(flag);
    }

    @ApiOperation(value = "用户等级新增接口")
    @PostMapping("/insert")
    public Integer insert(@RequestBody LevelInfo levelInfo){
        return levelInfoService.insert(levelInfo);
    };

    @ApiOperation(value = "用户等级更新接口")
    @PostMapping("/updateById")
    public boolean updateById(@RequestBody LevelInfo levelInfo){
        return levelInfoService.updateById(levelInfo);
    };

    @ApiOperation(value = "VIp等级删除接口")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer levelId){
        boolean flag = false;
        if (levelId > 0) {
            flag = levelInfoService.removeLevel(levelId);
        }
        return DResult.of(flag);
    }

}
